#include "app_mqtt.h"

#define MQTT_SERVER_URL "tcp://192.168.55.228:1883"
#define MQTT_CLIENT_ID "gate_mqtt_client"
#define MQTT_TOPIC_PULL "pull"
#define MQTT_TOPIC_PUSH "push"

typedef struct
{
    MQTTClient client;
    void (*msg_cb)(char *data, int len);
} app_mqtt_t;

static app_mqtt_t my_mqtt;

void conn_lost(void *context, char *cause);
int msg_arrive(void *context, char *topicName, int topicLen, MQTTClient_message *message);
void delivery_complete(void *context, MQTTClient_deliveryToken dt);
gate_status_t app_mqtt_init(void (*msg_cb)(char *data, int len))
{
    my_mqtt.msg_cb = msg_cb;
    // 1. 创建mqtt客户端
    if (MQTTClient_create(&my_mqtt.client,
                          MQTT_SERVER_URL,
                          MQTT_CLIENT_ID,
                          MQTTCLIENT_PERSISTENCE_NONE,
                          NULL) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_create failed");
        return GATE_ERROR;
    }
    log_info("MQTTClient_create success");
    // 2. 设置回调函数
    if (MQTTClient_setCallbacks(my_mqtt.client,
                                NULL,
                                conn_lost,
                                msg_arrive,
                                delivery_complete) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_setCallbacks failed");
        return GATE_ERROR;
    }
    log_info("MQTTClient_setCallbacks success");
    // 3. 连接服务器
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    if (MQTTClient_connect(my_mqtt.client, &conn_opts) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_connect failed");
        return GATE_ERROR;
    }

    // 4. 订阅主题
    if (MQTTClient_subscribe(my_mqtt.client, MQTT_TOPIC_PULL, 0) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_subscribe failed");
        return GATE_ERROR;
    }
    log_info("MQTTClient_subscribe success");
    return GATE_OK;
}

void app_mqtt_deinit(void)
{
    // 0. 取消订阅主题
    MQTTClient_unsubscribe(my_mqtt.client, MQTT_TOPIC_PULL);
    // 1. 断开连接
    MQTTClient_disconnect(my_mqtt.client, 1000);

    // 2. 销毁mqtt客户端
    MQTTClient_destroy(&my_mqtt.client);
}

void conn_lost(void *context, char *cause)
{

    while (1)
    {
        MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
        if (MQTTClient_connect(my_mqtt.client, &conn_opts) != MQTTCLIENT_SUCCESS)
        {
            log_error("MQTTClient_connect failed");
            sleep(2);
            continue;
        }
        log_info("MQTTClient_connect success");
        break;
    }

    while (1)
    {

        if (MQTTClient_subscribe(my_mqtt.client, MQTT_TOPIC_PULL, 0) != MQTTCLIENT_SUCCESS)
        {
            log_error("MQTTClient_subscribe failed");
            sleep(2);
            continue;
        }
        log_info("MQTTClient_subscribe success");
        break;
    }
}
int msg_arrive(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    // log_info("内容: %.*s", message->payloadlen, message->payload);
    if (my_mqtt.msg_cb)
    {
        my_mqtt.msg_cb(message->payload, message->payloadlen);
    }
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1; // 表示数据被正确处理
}
void delivery_complete(void *context, MQTTClient_deliveryToken dt)
{
    log_info("delivery_complete ....");
}

gate_status_t app_mqtt_send(char *data, int len)
{
    if (MQTTClient_publish(my_mqtt.client, MQTT_TOPIC_PUSH, len, (void *)data, 0, 0, NULL) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_publish error");
        return GATE_ERROR;
    }
    log_info("MQTTClient_publish ok");
    return GATE_OK;
}
